#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"
extern void Mipi_Read_U16(USB_OTG_CORE_HANDLE *pdev,u8 SigMode ,u8 channel,u8 regpage,u8 regaddr ,u8 backnum, u8* Rxbuffer);
//********************************************************************************************   
//1:Flash ID Read;
void TC1105_Enable_Flash_Control_and_SPI_Flash_En(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{

		//u8 buffer1[3];
	u8 Read_Flash_ID_1st_byte_Set[40]={0xDF,0xEC,0x5F,0x80,0xDF,0xEC,0x78,0xF9,0xDF,0xEC,0xE0,0x04,
	0xDF,0xE0,0x6E,0x72,0xDF,0xE0,0x6F,0x18,0xdf,0xe0,0x6e,0xbf,0xdf,0xe0,0x6f,0x01,
	0xDF,0xEC,0xD0,0x9F,0xDF,0xEC,0xE1,0x20,0xDF,0xEC,0xE2,0x03};
	u8 Read_Flash_exit_Bypass_Set[12]={0xdf,0xEC,0x5f,0x00,0xdf,0xEc,0xe0,0x08,0xdf,0xEc,0xe0,0x00};
	
	{
		u8 buffer1[6];
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
		delay_ms(15);
		buffer1[0] = 0x04;
		for(i=0;i<sizeof(Read_Flash_ID_1st_byte_Set);i++)
		{
			buffer1[1] = Read_Flash_ID_1st_byte_Set[i];
			buffer1[2] = Read_Flash_ID_1st_byte_Set[i+1];
			buffer1[3] = Read_Flash_ID_1st_byte_Set[i+2];
			buffer1[4] = Read_Flash_ID_1st_byte_Set[i+3];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			if((buffer1[3]==0x6f)&&(buffer1[4]==0x18))
			{
		    	delay_ms(120);
			}
			else
			{
			    delay_ms(5);
			}
			
			i+=3;
		}
		delay_ms(5);
		SSD2828_W_Reg(SigMode,channel,0xB7,0x0280);	 	      	
		delay_ms(15);

		Mipi_Read_U16(pdev,SigMode,channel,0xEC,0xF0,1,Buffer_ReadCode);
		buffer[4]=Buffer_ReadCode[0];	;     //返回第一个字节；  
		delay_ms(5);
		Mipi_Read_U16(pdev,SigMode,channel,0xEC,0xF1,1,Buffer_ReadCode);
		buffer[4+1]=Buffer_ReadCode[0];	;     //返回第二个字节； 
		delay_ms(5);
		Mipi_Read_U16(pdev,SigMode,channel,0xEC,0xF2,1,Buffer_ReadCode);
		buffer[4+2]=Buffer_ReadCode[0];	;     //返回第三个字节； 
 
		SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
		delay_ms(15);
		for(i=0;i<sizeof(Read_Flash_exit_Bypass_Set);i++)     //exit bypass
		{
			buffer1[0] = 0x04;
			buffer1[1] = Read_Flash_exit_Bypass_Set[i];
			buffer1[2] = Read_Flash_exit_Bypass_Set[i+1];
			buffer1[3] = Read_Flash_exit_Bypass_Set[i+2];
			buffer1[4] = Read_Flash_exit_Bypass_Set[i+3];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=3;
		}

		buffer[0] = 0x2F;
		buffer[1] = 0x01;
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	}
}

//2:Flash Setting &De-Mura Ram Enable;
void TC1105_FlashSetting(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		    u8 buffer1[5];
	      u8 bufferTemp;
	
			  SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);   //切写
		    delay_ms(15);
//				buffer1[0]=0x01;
//				buffer1[1]=0x29;
//				SSD2828_W_Array(SigMode,channel,buffer1,0);
//				delay_ms(1000);
						
				buffer1[0]=0x04;
				buffer1[1]=0xDF;
			  buffer1[2]=0xEC;
				buffer1[3]=0x8E;
				buffer1[4]=0xB1;
				SSD2828_W_Array(SigMode,channel,buffer1,0);
				delay_ms(5);
						
				SSD2828_W_Reg(SigMode,channel,0xB7,0x0280);
		    delay_ms(15);
						
				Mipi_Read_U16(pdev,SigMode,channel,0xEC,0x8F,1,Buffer_ReadCode);
			  bufferTemp=(Buffer_ReadCode[0]&0xF0)>>4;      
				delay_ms(5);
				if(bufferTemp !=0x05)
				{
								buffer[3] = 0x03;   //8F  [4-7]  不正确   
				        buffer[4] = bufferTemp;
				}
				buffer[0] = 0x2F;
		    buffer[1] = 0x02;
		    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
}

                                    
//3.load data
void TC1105_Flash_Demura_Data_Load(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{

	      SSD2828_W_Reg(SigMode,channel,0xb9,0x00);      
	      delay_ms(200);
				SSD2828_W_Reg(SigMode,channel,0xba,0xC6fd);
  	    delay_ms(1000);
		    SSD2828_W_Reg(SigMode,channel,0xb9,0x01);      
	      delay_ms(200);
	

	
			buffer[0] = 0x2F;
		  buffer[1] = 0x08;		
		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	  delay_ms(15);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);    

}

//4:check demura data
void TC1105_Flash_RAM_Disable(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	u8 bufferTemp;
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	delay_ms(15);
	buffer1[0] = 0x02;
	buffer1[1] = 0xF1;
	buffer1[2] = 0x00;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(200);
	buffer1[0] = 0x04;
	buffer1[1] = 0xdf;
	buffer1[2] = 0xec;
	buffer1[3] = 0x8e;
	buffer1[4] = 0xa8;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);
		
	{		
		
		  SSD2828_W_Reg(SigMode,channel,0xB7,0x0280);
		  delay_ms(15);
		
		  Mipi_Read_U16(pdev,SigMode,channel,0xEC,0x8F,1,Buffer_ReadCode);
		  bufferTemp=Buffer_ReadCode[0];      
		  delay_ms(5);
		
		  if((bufferTemp&0x0F) != 4)
			{
				 buffer[3] = 0x01;   //8F  不正确   
				 buffer[4] = bufferTemp;
			}
			else
			{
			    Mipi_Read_U16(pdev,SigMode,channel,0xEC,0xED,1,Buffer_ReadCode);
		      bufferTemp=Buffer_ReadCode[0];      
		      delay_ms(5);
				  if(bufferTemp !=0x10)
					{
					    	buffer[3] = 0x02;   //ED  不正确   
				        buffer[4] = bufferTemp;
					}
					
			}
		
		buffer[0] = 0x2F;
		buffer[1] = 0x04;		
		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);    
	}	

}

                                    
//5.Flash Erase
void TC1105_Flash_Erase(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
  u8 Erase_Flash_byte_Set[44]={0xDF,0xEC,0x5F,0x80,0xDF,0xEC,0x78,0xF9,0xDF,0xEC,0xE0,0x04,
	0xDF,0xE0,0x6E,0x72,0xDF,0xE0,0x6F,0x18,0xdf,0xec,0xd0,0x06,0xdf,0xec,0xe1,0x00,
	0xDF,0xEC,0xE2,0x01,0xDF,0xEC,0xD0,0xC7,0xDF,0xEC,0xE1,0x00,0xDF,0xEC,0xE2,0x01};
	u8 Erase_Flash_exit_Bypass_Set[8]={0xdf,0xEC,0x5f,0x00,0xdf,0xEc,0xe0,0x00};
	
	{
		u8 buffer1[6];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
		delay_ms(15);
		buffer1[0] = 0x04;
		for(i=0;i<sizeof(Erase_Flash_byte_Set);i++)
		{
			buffer1[1] = Erase_Flash_byte_Set[i];
			buffer1[2] = Erase_Flash_byte_Set[i+1];
			buffer1[3] = Erase_Flash_byte_Set[i+2];
			buffer1[4] = Erase_Flash_byte_Set[i+3];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=3;
		}
		delay_ms(50000);  //50s  等待
 
		for(i=0;i<sizeof(Erase_Flash_exit_Bypass_Set);i++)     //exit bypass
		{
			buffer1[0] = 0x04;
			buffer1[1] = Erase_Flash_exit_Bypass_Set[i];
			buffer1[2] = Erase_Flash_exit_Bypass_Set[i+1];
			buffer1[3] = Erase_Flash_exit_Bypass_Set[i+2];
			buffer1[4] = Erase_Flash_exit_Bypass_Set[i+3];
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
			i+=3;
		}	
		buffer[0] = 0x2F;
		buffer[1] = 0x05;		
		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);
	
   }
}                           


//6.Flash Write De-Mura Data
void TC1105_Flash_Write_Demura_Data(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
}
//7.加载demura配置文件数据
void TC1105_Flash_Reload_Demura_Data(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		
}
//8.CRC Check
void TC1105_Flash_Demura_Data_CRC(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	//IC Reload flash data CRC page6_B7/B8
	//Calc_CRC_Page6_BB/BC
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0410)&0XFFFD);
	delay_ms(15);	
		
	{
		{
			buffer1[0]=0x04;
			buffer1[1]=0xFF;
			buffer1[2]=0x78;
			buffer1[3]=0x35;
			buffer1[4]=0x06;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(3);
		}		
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); //
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);	
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package 		 	                          
		buffer1[0] = 0x01;
		buffer1[1] = 0xB7;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(3);
		buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);     //返回；CRC
		buffer1[0] = 0x01;
		buffer1[1] = 0xB8;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(3);
		buffer[5]=SSD2828_R_Reg(SigMode,channel,0xFF);
		buffer1[0] = 0x01;
		buffer1[1] = 0xBB;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(3);
		buffer[6]=SSD2828_R_Reg(SigMode,channel,0xFF);     //返回；CRC
		buffer1[0] = 0x01;
		buffer1[1] = 0xBC;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(3);
		buffer[7]=SSD2828_R_Reg(SigMode,channel,0xFF);
		
		buffer[0] = 0x2F;
		buffer[1] = 0x08;			
			
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);   
	}
}
//9.De-Mura Function On
void TC1105_Demura_Function_ON(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	//PageA 90,93
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	delay_ms(15);
	{

		
			buffer1[0]=0x04;
			buffer1[1]=0xDF;
			buffer1[2]=0xEC;
			buffer1[3]=0x5F;
			buffer1[4]=0x00;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(5);
		


		buffer[0] = 0x2F;
		buffer[1] = 0x09;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	}  
}

//A.De-Mura Function Off
void TC1105_Demura_Function_OFF(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[5];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_DCS_Short_Write);
	delay_ms(15);
	{	
			buffer1[0]=0x04;
			buffer1[1]=0xDF;
			buffer1[2]=0xEC;
			buffer1[3]=0x5F;
			buffer1[4]=0x80;
			SSD2828_W_Array(SigMode,channel,buffer1,0);
			delay_ms(3);
		


		buffer[0] = 0x2F;
		buffer[1] = 0x0A;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);  
	}       
}

void ProcessForDmuR20( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      // 
					TC1105_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
					//TC1105_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      // 
					TC1105_FlashSetting(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
					//TC1105_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      // 
					TC1105_Flash_Demura_Data_Load(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      // 
					TC1105_Flash_RAM_Disable(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      // 
					TC1105_Flash_Erase(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      // 
					TC1105_Flash_Write_Demura_Data(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);				
			break;
			case 0x07:                                      // 
					TC1105_Flash_Reload_Demura_Data(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x08:                                      // 
					TC1105_Flash_Demura_Data_CRC(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;			
			case 0x09:                                      // 
					TC1105_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x0A:                                      // 
					TC1105_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			
			default:
				break;												
	}
}
//********************************************************************************************v